﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL;
using Entities;
using BUS;

public partial class A_LetsDeal_giohang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string yeu_cau = Request.QueryString.ToString();
        if (yeu_cau.Contains("id"))
        {
            GIOHANG_Entities i = new GIOHANG_Entities();
            Tbl_SanPham_Entities sanpham = Tbl_SanPham_BUS.GetByID(int.Parse(Request.QueryString["id"].ToString()));
            i.STT_sanpham = sanpham.STT;
            i.TenSP = sanpham.TenSP;
            i.GiaBan = sanpham.GiaBan;
            i.SOLUONG = 1;
            i.THANHTIEN = sanpham.GiaBan;
            GIOHANG_DAL c;
            if (Session["Cart"] == null)
            {
                c = new GIOHANG_DAL();
                c.Add(i);
            }
            else
            {
                c = (GIOHANG_DAL)Session["Cart"];
                c.Add(i);
            }
            Session["Cart"] = c;
            Response.Redirect(Request.UrlReferrer.AbsoluteUri);
        }
        else
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }


    }

    private void LoadData()
    {
        GIOHANG_DAL c = (GIOHANG_DAL)Session["Cart"];
        if (Session["Cart"] != null)
        {
            Repeater1.DataSource = c.ItemCollection;
            Repeater1.DataBind();
        }
        else
        {
            Literal1.Text = "Giỏ hàng của bạn còn trống";
        }
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        GIOHANG_DAL c = (GIOHANG_DAL)Session["Cart"];
        if (e.Item.ItemType == ListItemType.Footer)
        {
            Literal lit_tongtrigia = (Literal)e.Item.FindControl("lit_tongtrigia");
            lit_tongtrigia.Text = String.Format("{0:0,0 đ}",c.Total());
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["Cart"] = new GIOHANG_DAL();
        Response.Redirect("~/A_LetsDeal/giohang.aspx");
    }

    protected void click_ne(object sender, EventArgs z)
    {
        GIOHANG_DAL c = (GIOHANG_DAL)Session["Cart"];
        int n = c.ItemCollection.Count;

        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            TextBox txt_sl = (TextBox)Repeater1.Items[i].FindControl("txt_sl");
            int sl = int.Parse(txt_sl.Text);
            if (sl == 0)
            {
                sl = 1;
            }
            else
            {
                Tbl_SanPham_Entities sp = Tbl_SanPham_BUS.GetByID(c.ItemCollection[i].STT_sanpham);
                int slvc = Tbl_CTDH_BUS.CountByIDSP(c.ItemCollection[i].STT_sanpham);
                if (sl > (sp.SLBan - slvc))
                {
                    sl = sp.SLBan - slvc;
                }
            }
            c.ItemCollection[i].SOLUONG = sl;
            c.ItemCollection[i].THANHTIEN = c.ItemCollection[i].SOLUONG * c.ItemCollection[i].GiaBan;
        }
        Session["Cart"] = c;
        Response.Redirect("~/A_LetsDeal/giohang.aspx");
    }

    protected void bt_timkiem_Click(object sender, EventArgs e)
    {
        if (txt_timkiem.Text != "")
        {
            Response.Redirect("~/A_LetsDeal/Default.aspx?tensanpham=" + txt_timkiem.Text);
        }
    }
}
